$(function(){
    function popFn(type , id){
        popHtml = ''
        popHtml += '<div id="graybg_pop" class="graybg_pop ie6fixed_t" style="display:block"></div>'
        if(type != 2){
        popHtml += '<div class="col-lg-3 teacher_pop friend_pop student_pop" style="display:block">'    
        }else{
        popHtml += '<div class="col-lg-3 teacher_pop friend_pop student_pop" style="height:200px;margin-top:-100px;display:block">'
        }
        popHtml += '    <span class="teacher_tit">添加</span>'
        popHtml += '    <i class="fa fa-times" aria-hidden="true"></i>'
        if(type != 2){
        popHtml += '    <div class="form-group">'
        popHtml += '        <label>姓名</label>'
        popHtml += '        <input class="form-control namea" value="" placeholder="请输入姓名">'    
        popHtml += '    </div>'
        popHtml += '    <div class="form-group">'
        popHtml += '        <label>手机</label>'
        popHtml += '        <input class="form-control tel"  placeholder="请输入手机">'
        popHtml += '    </div>'
        popHtml += '    <div class="form-group">'
        popHtml += '        <label>身份证</label>'
        popHtml += '        <input class="form-control ssf"  placeholder="请输入身份证">'
        popHtml += '    </div>'
        }
        popHtml += '    <div class="form-group">'
        popHtml += '        <label>密码</label>'
        popHtml += '        <input class="form-control pass" type="password"  placeholder="请输入密码">'
        popHtml += '    </div>'
        popHtml += '    <p class="p_btn"><button type="button" class="btn btn-lg btn-success add_ok" data_id="'+ id +'" data_type="'+ type +'">确定</button></p>'
        popHtml += '</div>'
    }

    function kaitFn(data , id){
        kaiHTml = ''
        kaiHTml += '<div id="graybg_pop2" class="graybg_pop ie6fixed_t" style="display:block"></div>'
        kaiHTml += '<div id="select2" class="col-lg-3 teacher_pop friend_pop " style="height:200px;margin-top:-100px;display:block">'
        kaiHTml += '    <span class="teacher_tit">课程开通</span>'
        kaiHTml += '    <i class="fa fa-times kaiclose" aria-hidden="true"></i>'
        kaiHTml += '    <div class="form-group">'
        kaiHTml += '        <label>Selects</label>'
        kaiHTml += '        <select class="form-control">'
        for(var i=0;i<data.length;i++){
            kaiHTml += '       <option data_id="'+ data[i].id +'"=>'+ data[i].name +'</option>'
        }
        kaiHTml += '        </select>'
        kaiHTml += '    </div>'

        kaiHTml += '    <p class="p_btn"><button type="button" class="btn btn-lg btn-success kaitongBtn" data_id="'+ id +'" data_type="">确定</button></p>'
        kaiHTml += '</div>'
    }

    admin.ajaxFn(
        {
            url:admin.baseUrl.studentUserList,
            type : 'GET',
            data : {
                page_size : 20,
                page : 1
            }
        },
        function(data){
            $('#student').html(template('studentTpl',{'res': data}));
        }
    )


    //修改状态
    $('body').on( 'click' ,'.status_btn' , function(){
        var dataid = $(this).attr('data_id')
        var datatype = $(this).attr('data_type')
        admin.ajaxFn(
            {
                url:admin.baseUrl.studentUserListDisable,
                type : 'POST',
                data : {
                    id : dataid,
                    status : datatype
                }
            },
            function(data){
                alert('修改成功！')
                //友情列表
                admin.ajaxFn(
                    {
                        url:admin.baseUrl.studentUserList,
                        type : 'GET',
                        data : {
                            page_size : 20,
                            page : 1
                        }
                    },
                    function(data){
                        $('#student').html(template('studentTpl',{'res': data}));
                    }
                )
            }
        )
    })

    //添加
    $('body').on( 'click' ,'.add_list' , function(){
        popFn(1 , '');
        $('body').append(popHtml)

    })

    //修改
    $('body').on( 'click' ,'.mod_btn' , function(){
        var dataid = $(this).attr('data_id')
        popFn(2 , dataid);
        $('body').append(popHtml)

    })

    //课程开通弹窗
    $('body').on( 'click' ,'.kait' , function(){
        var dataid = $(this).attr('data_id')
        admin.ajaxFn(
            {
                url:admin.baseUrl.studentUserListOpenAll,
                type : 'POST',
                data : {
                    student_id : dataid
                }
            },
            function(data){
                kaitFn(data , dataid);
                $('body').append(kaiHTml)
            }
        )
    })

    //课程开通
    $('body').on( 'click' ,'.kaitongBtn' , function(){
        var dataid = $('.form-control option:selected').attr('data_id')
        var btnid = $(this).attr('data_id')
        admin.ajaxFn(
            {
                url:admin.baseUrl.studentUserListOpen,
                type : 'POST',
                data : {
                    g_id : dataid,
                    u_id : btnid
                }
            },
            function(data){
                alert('开通成功')
                $('#graybg_pop2 , #select2').remove();
            }
        )
    })


    //关闭
    $('body').on( 'click' ,'.kaiclose' , function(){
        $('#graybg_pop2 , #select2').remove();

    })
    
    //确定
    $('body').on( 'click' ,'.add_ok' , function(){
        var datatype = $(this).attr('data_type')
        var dataid = $(this).attr('data_id')
        var nameVal = $('.namea').val()
        var telVal = $('.tel').val()
        var ssfVal = $('.ssf').val()
        var telTest = /^1(3|4|5|6|7|8|9)\d{9}$/
        var nameTest = /^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$/
        var card = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if(datatype == 1){
            if(nameVal.length <= 0){
                alert('请输入姓名')
                return false
            }else if(!(nameTest.test(nameVal))){
                alert('姓名请填写中文')
                return false
            }else if(!(nameVal.length >= 1 && nameVal.length <=5)){
                alert('请填写1到5个字！')
                return false
            }else if(telVal.length <= 0){
                alert('请输入手机')
                return false
            }else if(!(telTest.test(telVal))){
                alert('请填写正确手机号！')
                $('.tel').val("")
                return false
            }else if(ssfVal.length <= 0){
                alert('请输入身份证')
                return false
            }else if(card.test(ssfVal) === false){
                alert("请输入正确身份证");
                $("#ssf").val("");
                return false
            }else if($('.pass').val().length <= 0 ){
                alert('请输入密码')
                return false
            }else if($('.pass').val().length < 10){
                alert('密码最少10位数')
                return false
            }else{
                admin.ajaxFn(
                    {
                        url:admin.baseUrl.studentUserListAdd,
                        type : 'POST',
                        data : {
                            name : $('.namea').val(),
                            phone : $('.tel').val(),
                            id_card : $('.ssf').val(),
                            password : $('.pass').val()
                        }
                    },
                    function(data){
                        admin.ajaxFn(
                            {
                                url:admin.baseUrl.studentUserList,
                                type : 'GET',
                                data : {
                                    page_size : 20,
                                    page : 1
                                }
                            },
                            function(data){
                                $('#graybg_pop , .teacher_pop').remove();
                                alert('添加成功')
                                $('#student').html(template('studentTpl',{'res': data}));
                            }
                        )
                    }
                )
            }
        }else{
            if($('.pass').val().length <= 0 ){
                alert('请输入密码')
                return false
            }else if($('.pass').val().length < 10){
                alert('密码最少10位数')
                return false
            }else{
                admin.ajaxFn(
                    {
                        url:admin.baseUrl.studentUserListUpdate,
                        type : 'POST',
                        data : {
                            id :dataid,
                            password : $('.pass').val()
                        }
                    },
                    function(data){
                        $('#graybg_pop , .teacher_pop').remove();
                        alert('修改成功')
                    }
                )
            }
            
        }

    })
    



})